package com.cdg.process.factory;

import java.util.Map;

import com.cdg.process.common.ProcessConstants.ProcessType;
import com.cdg.process.service.ProcessService;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ProcessServiceFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ProcessService getProcessService(ProcessType processType){
        Map<String, ProcessService> processTypeMap = applicationContext.getBeansOfType(ProcessService.class);
        for(ProcessService processService : processTypeMap.values()){
            if(processType == processService.getHandledProcessType()){
                return processService;
            }
        }
        throw new RuntimeException("Can not get process service by type " + processType);
    }
}
